

EnemyController = {
		intervalX: 0,
		intervalZ: 0,
		attackRange: 10000,

		positionUpdate: function(){

				for(var i=0; i<Enemy.enemyList.length; i++){
					if(i<Enemy.enemyList.length){
						EnemyController.setDirection(i);
						var xspeed = Math.random()*EnemyModel[i].speed.x;
						var zspeed = Math.random()*EnemyModel[i].speed.y;
						EnemyModel[i].pos.x +=  EnemyModel[i].direction.x * xspeed;
						EnemyModel[i].pos.z +=  EnemyModel[i].direction.z * zspeed;
						Enemy.enemyList[i].position.set(EnemyModel[i].pos.x, EnemyModel[i].pos.y, EnemyModel[i].pos.z );
						EnemyController.fire(i);
					}
				}
			
		},
		setDirection: function(i){
			if(EnemyModel[i].pos.x >= World.floorWidth/2 ||
					EnemyModel[i].pos.x <  -World.floorWidth/2){
				EnemyModel[i].direction.x = -EnemyModel[i].direction.x;
			}else if(false){
				if(EnemyModel[i].directionWeight.x > 0){
					EnemyModel[i].directionWeight.x -= 1;
				}else{
					EnemyModel[i].directionWeight.x = 100;
					var tmpDirection = 5 - Math.random()*10;				
					if( (tmpDirection >= 0 && EnemyModel[i].direction.x < 0) ||
							(tmpDirection <= 0 && EnemyModel[i].direction.x > 0)
					){
						EnemyModel[i].direction.x = - EnemyModel[i].direction.x;
					}
				}
			}
			if( EnemyModel[i].pos.z >= World.floorHeight/2 ||
					EnemyModel[i].pos.z  <= - World.floorHeight/2){
				EnemyModel[i].direction.z = -EnemyModel[i].direction.z;
			}else if(false){
				if(EnemyModel[i].directionWeight.z > 0){
					EnemyModel[i].directionWeight.z -= 2;
				}else{
					EnemyModel[i].directionWeight.z = 100;
					var tmpDirection = 5 - Math.random()*10;				
					if( (tmpDirection >= 0 && EnemyModel[i].direction.z < 0) ||
							(tmpDirection <= 0 && EnemyModel[i].direction.z > 0)
					){
						EnemyModel[i].direction.z = - EnemyModel[i].direction.z;
					}
				}
			}
		},

		fire: function(i){

//			var x1 = EnemyModel[i].pos.x;
//			var y1 = EnemyModel[i].pos.y;
//			var z1 = EnemyModel[i].pos.z;
//			var x0 = PlayMan.pos.x;
//			var y0 = PlayMan.pos.y;
//			var z0 = PlayMan.pos.z;
//			var ds = Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0) + (z1-z0)*(z1-z0));
//			if(ds <= this.attackRange){

				var fire = Math.random() * 100;
//				console.log("---------fire:" + fire);
				// type = 0 , check whether there is obstacle between enemy and player.
				if(true && fire < 5 /*&& !ObstacleCheck.collide(i,0)*/){
//					console.log("you are attacked by " + i);
					GunVoiceContrller.enemyFire(i);
					
					var target = Math.random() * 100;
					if(target < 90 ){
						GunVoiceContrller.playerShooted();
						TopPaneStatus.updateBood();
//						console.log("you are shooted!!!!!!!!!!!!");
						MainArea.shooted();
					}
				}
//			}
		},
}